package com.mindcraft.user.api;


import com.mindcraft.base.model.MindCraftResult;
import com.mindcraft.user.Dto.QQLoginDto;
import com.mindcraft.user.serivce.QQAuthService;
import com.mindcraft.user.vo.QQUserVO;
import com.mindcraft.user.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@Slf4j
@Controller
@RequestMapping("/user")
@Api(value = "user", tags = "QQ用户管理接口")
public class QQLoginController {
    @Autowired
    private QQAuthService qqAuthService;

    @RequestMapping("qqShowCode")
    public ResponseEntity<byte[]> getImage() throws IOException {


        // 读取图片文件
        File file = new File("QQToolChest.png");
        FileInputStream inputStream = new FileInputStream(file);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }

        // 获取字节数组
        byte[] bytes = outputStream.toByteArray();

        // 关闭流
        inputStream.close();
        outputStream.close();

        // 构建响应体
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_JPEG);
        headers.setContentLength(bytes.length);
        return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
    }

    @GetMapping("qqcheckcode")
    @ResponseBody
    public MindCraftResult qqcheck(){


        MindCraftResult qqcheckMessage = qqAuthService.qqlogin();

        return qqcheckMessage;
    }


    @PostMapping("getqqLoginInfo")
    @ResponseBody
    @ApiOperation("利用qq号获取qq用户信息")
    public MindCraftResult getqqLoginInfo(@RequestParam("qqacount") String qqacount){
        QQUserVO qqUserVO = qqAuthService.getloginInfo(qqacount);
        return MindCraftResult.success(qqUserVO);
    }



}